/*
 * @Author: szx
 * @Date: 2023-03-29 18:58:30
 * @LastEditTime: 2023-03-30 17:22:15
 * @Description: 
 * @FilePath: \learn\fullstackopen\part3\mongo.js
 */
const mongoose = require('mongoose')


const url =
  `mongodb://admin:admin@localhost:27017/test?authSource=admin&readPreference=primary&ssl=false&directConnection=true`

mongoose.connect(url)

const personSchema = new mongoose.Schema({
  name: String,
  number: String,
})

personSchema.set('toJSON', {
  transform: (document, returnedObject) => {
    returnedObject.id = returnedObject._id.toString()
    delete returnedObject._id
    delete returnedObject.__v
  }
})
const Person = mongoose.model('PhoneBook', personSchema)

const person = new Person({
  name: 'ns',
  number: 123
})
// module.exports = Person;
// const person = new Person({
//     name: process.argv[3],
//     number: process.argv[4],
// })

person.save().then(result => {
  console.log('person saved!')
  mongoose.connection.close()
})

// Person.find({}).then(result => {
//     result.forEach(note => {
//         console.log(note)
//     })
//     mongoose.connection.close()

// })